//
//  SelectSeatBodyView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/13.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class SelectSeatBodyView: UIView {
    
    // 数量
    private let num = 119
    private let columns = 7
    // 宽度、高度
    private let btnW = MARGIN25
    private let btnH = MARGIN25
    // 滑动View
    private var scrollview = UIScrollView()
    // 常量
    private let nums = ["A","B","C","D","E","F","G"]
    
    // 订单信息
    public var orderInfo:OrderInfo? {
        didSet {
            setNeedsLayout()
        }
    }
    
    // 座位不能交互
    public var imgIsUserInteractionEnabled:Bool = true
    
    // 座位号
    public var selectedIndex:[String]?
    
    // 回调
    public var complete:((_ seat:String,_ seatIndex:Int) ->Void)?
    
    override func layoutSubviews() {
        guard let order = orderInfo else {
            return
        }
        // 间距
        let margin = (frame.width - CGFloat(columns) * btnW) / CGFloat(columns + 1)
        // 中间序号
        var j = 0
        // 序号
        for i in 0..<columns {
            if i == 3 {
                continue
            }
            let label = UILabel(title: "", color: COLOR_NICE, align: .center)
            label.text = nums[i]
            label.font = UIFont.boldSystemFont(ofSize: 14)
            let btnX = CGFloat(i % columns) * (margin + btnW) + margin
            label.frame = CGRect(x: btnX, y: margin * 0.5, width: btnW, height: btnH)
            scrollview.addSubview(label)
        }
        // 记录高度
        var tempHeight:CGFloat = 0
        // 公务舱
        for i in 0..<num {
            // 坐标
            let btnX = CGFloat(i % columns) * (margin + btnW) + margin
            let btnY = CGFloat(i / columns) * (margin * 0.5 + btnH) + margin * 1.5
            // 中间挖空
            if i == 3 + j * 7 {
                j += 1
                // 舱位号
                let label = UILabel(title: "\(j)", color: UIColor.gray, size: 15, align: .center)
                label.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
                scrollview.addSubview(label)
                continue
            }
            let img = UIImageView(image: UIImage(named: "unselect"))
            img.tag = i
            if imgIsUserInteractionEnabled {
                img.isUserInteractionEnabled = true
            }
            if order.cabinname!.elementsEqual("公务舱") ,i > 35 {
                // 公务舱
                img.isUserInteractionEnabled = false
                img.image = UIImage(named: "seatlock")
            }
            if order.cabinname!.elementsEqual("经济舱") ,i < 35 {
                // 经济舱
                img.isUserInteractionEnabled = false
                img.image = UIImage(named: "seatlock")
            }
            // 紧急出口
            if i == 35 || i == 41 || i == 91 || i == 97 {
                img.isUserInteractionEnabled = false
                img.image = UIImage(named: "seatexit")
            }
            // 设置已选中
            if let indexs = selectedIndex , indexs.contains(String(describing: i)) {
                img.isUserInteractionEnabled = false
                img.image = UIImage(named: "seatlock")
            }
            // 摆放位置
            img.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            scrollview.addSubview(img)
            // 最后一个view高度
            if i == num - 1 {
                tempHeight = img.frame.maxY
            }
            // 添加点击事件
            img.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(clickItem(ui:))))
        }
        addSubview(scrollview)
        // 设置scrollview属性
        scrollview.showsVerticalScrollIndicator = false
        scrollview.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollview.contentSize = CGSize(width: frame.width, height: tempHeight + MARGIN20)
    }
    
    @objc func clickItem(ui:UITapGestureRecognizer) -> Void {
        // 其它已选中改为未选中
        for item in subviews {
            for subItem in item.subviews {
                if subItem.isKind(of: UIImageView.self) {
                    let itemImg = subItem as! UIImageView
                    if itemImg.image == UIImage(named: "selected") {
                        itemImg.image = UIImage(named: "unselect")
                    }
                }
            }
        }
        // 选中
        let img = ui.view as! UIImageView
        img.image = UIImage(named: "selected")
        guard let index = ui.view?.tag else {
            return
        }
        // 列
        let col = index % columns
        // 行
        let row = index / columns + 1
        // 回调
        if let callback = complete {
            callback("\(nums[col])\(row)",index)
        }
    }
}
